﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SgLib;

public class DestroyObject : MonoBehaviour
{
    public float destroyTime = 1f;

    void OnEnable()
    {
        GameManager.GameStateChanged += GameManager_GameStateChanged;
    }

    void OnDisable()
    {
        GameManager.GameStateChanged -= GameManager_GameStateChanged;
    }


    void GameManager_GameStateChanged(GameState newState, GameState oldState)
    {
        if (newState == GameState.Playing)
        {

        }
        else if (newState == GameState.PreGameOver)
        {
            // Before game over, i.e. game potentially will be recovered
        }
        else if (newState == GameState.GameOver)
        {
            Destroy(gameObject);
        }
    }
    // Use this for initialization
    void Start()
    {
        Invoke("Disappear", destroyTime);
    }
	


    // Update is called once per frame
    void Disappear()
    {
        Destroy(gameObject);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            CoinManager.Instance.AddCoins(1);
            SoundManager.Instance.PlaySound(SoundManager.Instance.coin);
            Destroy(gameObject);           
        }
    }
}
